<?php

namespace console\controllers;

use yii\console\Controller;
use common\components\UpdateCache;

/**
 * 系统启动时初始化的缓存
 * 
 * yii init-cahce
 * 
 * yii intt-cahce "model class name"
 * 
 * @author kai.gao
 *
 */
class InitCacheController extends Controller{
	
	public function init() {
		parent::init();
		set_time_limit(0);
		ini_set('memory_limit', '512M');
	}
	
	/**
	 * 初始化缓存
	 * 
	 * @Title: actionIndex
	 * @Description:命令行初始化缓存
	 * @param string ModelName 
	 * @return: void
	 * @author: kai.gao
	 * @date: 2014-12-19下午12:02:56
	 */
	public function actionIndex($class_name = null) {
		$n = "\r\n";
		$data = f_p('cacheMap');
		if ($class_name != null) {
			if (!isset($data[$class_name])) {
				$this->stderr('class not found! You Have a Choice:'.$n);
				foreach ($data as $key=>$val) {
					$this->stdout($key.$n);
				}
				return;
			}
			$data = [$class_name => $data[$class_name]];
		}
		if (!$this->confirm('Are you sure flush cache?')) return;
		foreach ($data as $class_name=>$val) {
			echo $val['name'].' init.........'.$n;
			foreach ($val['mode'] as $method) {
				$name = [UpdateCache::className(), 'init'.$method];
				is_callable($name) && call_user_func($name, $class_name);
				echo $class_name.' '.$method.' init ok!'.$n;
			}
			echo $val['name'].' init ok!'.$n;
		}
	}
}

?>